using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    /// <summary>
    /// Preference screen
    /// </summary>
    public partial class FormPreference : FormBase
    {
        private System.Collections.Hashtable htPreference = new System.Collections.Hashtable();
        public bool bSaveChanges = false;

        public FormPreference()
        {
            InitializeComponent();
        }

        public void LoadPreference(System.Collections.Hashtable ht)
        {
            htPreference = (System.Collections.Hashtable) ht.Clone();

            foreach (string key in ht.Keys)
            {
                if (!lsv_Preference.IsItemAlreadyInList(key))
                {
                    ListViewItem aitem = new ListViewItem(key);
                    aitem.Tag = key;
                    aitem.SubItems.Add("" + ht[key]);

                    lsv_Preference.Items.Add(aitem);
                }
            }
        }

        public System.Collections.Hashtable SaveSetting()
        {
            foreach (ListViewItem aitem in lsv_Preference.Items)
            {
                if (aitem.Tag != null && !string.IsNullOrEmpty(aitem.Tag.ToString()))
                {
                    string key = aitem.Tag.ToString();
                    string value = aitem.SubItems[1].Text.Trim();

                    htPreference[key] = value;
                }
            }

            return this.htPreference;
        }

        private void btnSavePreference_Click(object sender, EventArgs e)
        {
            bSaveChanges = true;

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bSaveChanges = false;
            this.Close();
        }

        private void lsv_Preference_Click(object sender, EventArgs e)
        {
            if (lsv_Preference.SelectedItems.Count > 0)
            {
                ListViewItem aitem = lsv_Preference.SelectedItems[0];
                if (aitem.Tag != null && !string.IsNullOrEmpty(aitem.Tag.ToString()))
                {
                    string key = aitem.Tag.ToString();
                    string value = aitem.SubItems[1].Text;

                    txtValue.Text = value;
                    txtValue.Tag = key;
                }
            }
        }

        private void btnChangeField_Click(object sender, EventArgs e)
        {
            string FieldName = txtValue.Tag.ToString().Trim();
            string sValue = txtValue.Text.Trim();

            //update
            foreach (ListViewItem aitem in lsv_Preference.Items)
            {
                if (aitem.Tag != null && aitem.Tag.ToString() == FieldName)
                {
                    aitem.SubItems[1].Text = sValue;
                    return;
                }
            }
        }
    }
}